<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="被申请人" prop="reapplyId">
        <el-input
          v-model="queryParams.reapplyId"
          placeholder="请输入被申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="applyId">
        <el-input
          v-model="queryParams.applyId"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请下载成果名" prop="applyTitle">
        <el-input
          v-model="queryParams.applyTitle"
          placeholder="请输入申请下载成果名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div style="color: #343333;border: 1px solid #b4bccc;text-align: center;padding: 5px;background-color: #1ab394;border-radius: 10px;">我的申请</div>
    <el-row :gutter="10" class="mb8">
      <el-col class="table-container">
        <el-table v-loading="loading" :data="data1" @selection-change="handleSelectionChange" >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" type="index" />
          <el-table-column label="被申请人" align="center" prop="reapplyName" />
          <el-table-column label="申请下载成果名" align="center" prop="applyTitle" />
          <el-table-column label="申请状态" align="center" prop="applyStatus" >
            <template slot-scope="scope">
              <span v-if="scope.row.applyStatus==0">被驳回</span>
              <span v-if="scope.row.applyStatus==1">未通过</span>
              <span v-if="scope.row.applyStatus==2">已通过</span>
            </template>
          </el-table-column>
          <el-table-column label="下载状态" align="center" prop="downloadStatus" >
            <template slot-scope="scope">
              <span v-if="scope.row.applyStatus!=2"></span>
              <span v-else-if="scope.row.downloadStatus==0">未下载</span>
              <span v-else-if="scope.row.downloadStatus==1">已下载</span>
            </template>
          </el-table-column>
          <el-table-column label="下载记录数" align="center" prop="downloadCount" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.applyStatus==2"
                icon="el-icon-search"
                @click="handleLook(scope.row)"
                v-hasPermi="['work:record:edit']"
              >查看</el-button>
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.applyStatus==0"
                icon="el-icon-s-promotion"
                @click="handleReapply(scope.row)"
                v-hasPermi="['work:record:edit']"
              >重新申请</el-button>
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.applyStatus!=0"
                icon="el-icon-remove"
                @click="handleDelete(scope.row)"
                v-hasPermi="['work:record:remove']"
              >取消申请</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total1>0"
          :total="total1"
          :page.sync="queryParams.pageNum"
          :page-sizes="[5,10,15,20]"
          :limit.sync="queryParams.pageSize"
          layout="sizes,prev, pager, next, jumper"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <div style="color: #343333;margin: 20px 0px;border: 1px solid #b4bccc;text-align: center;padding: 5px;background-color: #1ab394;border-radius: 10px">申请我的</div>
    <el-row :gutter="10" class="mb8">
      <el-col class="table-container">
        <el-table v-loading="loading" :data="data2" @selection-change="handleSelectionChange" v-infinite-scroll="load">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" type="index" />
          <el-table-column label="申请人" align="center" prop="applyName" />
          <el-table-column label="申请下载成果名" align="center" prop="applyTitle" />
          <el-table-column label="申请状态" align="center" prop="applyStatus" >
            <template slot-scope="scope">
              <span v-if="scope.row.applyStatus==0">已驳回</span>
              <span v-if="scope.row.applyStatus==1">未通过</span>
              <span v-if="scope.row.applyStatus==2">已通过</span>
            </template>
          </el-table-column>
          <el-table-column label="下载状态" align="center" prop="downloadStatus" >
            <template slot-scope="scope">
              <span v-if="scope.row.downloadStatus==0">未下载</span>
              <span v-if="scope.row.downloadStatus==1">已下载</span>
            </template>
          </el-table-column>
          <el-table-column label="下载记录数" align="center" prop="downloadCount" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.applyStatus==1"
                icon="el-icon-edit"
                @click="handlePass(scope.row)"
                v-hasPermi="['work:record:edit']"
              >通过</el-button>
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.applyStatus!=0"
                icon="el-icon-delete"
                @click="handleBh(scope.row)"
                v-hasPermi="['work:record:remove']"
              >驳回</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total2>0"
          :total="total2"
          :page.sync="queryParams.pageNum"
          :page-sizes="[5,10,15]"
          :page-size="queryParams.pageSize"
          @pagination="getList1"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改成果申请下载记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="被申请人ID" prop="reapplyId">
          <el-input v-model="form.reapplyId" placeholder="请输入被申请人ID" />
        </el-form-item>
        <el-form-item label="申请人ID" prop="applyId">
          <el-input v-model="form.applyId" placeholder="请输入申请人ID" />
        </el-form-item>
        <el-form-item label="申请下载成果名" prop="applyTitle">
          <el-input v-model="form.applyTitle" placeholder="请输入申请下载成果名" />
        </el-form-item>
        <el-form-item label="下载记录数" prop="downloadCount">
          <el-input v-model="form.downloadCount" placeholder="请输入下载记录数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="成果查看" :visible.sync="openLook" width="1200px" append-to-body>
      <div>
        <span><award-detail :id="resultId" v-if="resultTypeId==1"></award-detail></span>
        <span><paper-detail :id="resultId" v-if="resultTypeId==2"></paper-detail></span>
        <span><patent-detail :id="resultId" v-if="resultTypeId==3"></patent-detail></span>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleDownload">导 出</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/work/record";
import {listUser} from "../../api/system/user";
import user from "../../store/modules/user";
import AwardDetail from "@/views/work/award/Detail/index.vue";
import PatentDetail from "@/views/work/patent/Detail/index.vue";
import paperDetail from "@/views/work/paper/Detail/index"

export default {
  name: "Record",
  components: {AwardDetail,PatentDetail,paperDetail},
  data() {
    return {
      openLook:false,
      resultId:'',
      resultTypeId:0,
      userID:user.state.id,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total1: 0,
      total2: 0,
      // 成果申请下载记录表格数据
      recordList: [],
      data1:[],
      data2:[],
      userList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        reapplyId: null,
        applyId: null,
        applyTitle: null,
        applyStatus: null,
        downloadStatus: null,
        downloadCount: null
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 5,
        reapplyId: null,
        applyId: null,
        applyTitle: null,
        applyStatus: null,
        downloadStatus: null,
        downloadCount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getList1();
  },
  methods: {
    /** 查询成果申请下载记录列表 */
    async getList() {
      this.data1=[];
      this.loading = true;
      let response = await listRecord(this.queryParams)
        this.recordList = response.rows;
      this.userList = (await listUser()).rows;

        for (const re of this.recordList) {
          if(re.reapplyId==this.userID){
            this.data1.push(re)
            this.total1 = this.total1+1;
          }
        }
      for (const data of this.data1) {
        for (const user of this.userList) {
          if(data.reapplyId==user.userId){
            data.reapplyName = user.nickName;
          }
        }
      }
        this.loading = false;

    },
    async getList1() {
      this.loading = true;
      let response = await listRecord(this.queryParams1)
        this.recordList = response.rows;

      this.userList = (await listUser()).rows;
        this.data2 = [];
        this.total2 = 0;
        this.recordList.forEach(re => {
          if (re.applyId == this.userID) {
            this.data2.push(re);
            this.total2 += 1;
          }
        });
        for (const data of this.data2) {
          for (const user of this.userList) {
            if(data.applyId==user.userId){
              data.applyName = user.nickName;
            }
          }
        }
        this.loading = false;

    },
    //通过按钮
    handlePass(row){
      console.log(row)
      row.applyStatus=2;
      updateRecord(row).then(response => {
        this.$modal.msgSuccess("通过成功");
        this.getList1();
      });
    },
    //驳回按钮
    handleBh(row){
      row.applyStatus=0;
      updateRecord(row).then(response => {
        this.$modal.msgSuccess("驳回成功");
        this.getList1();
      });
    },
    //重新申请
    handleReapply(row){
      row.applyStatus=1;
      updateRecord(row).then(response => {
        this.$modal.msgSuccess("申请成功");
        this.getList1();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reapplyId: null,
        applyId: null,
        applyTitle: null,
        applyStatus: null,
        downloadStatus: null,
        downloadCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams1.pageNum = 1;
      this.queryParams.pageNum = 1;
      this.getList();
      this.getList1();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.queryParams.reapplyId = null;
      // this.queryParams.applyId = null;
      // this.queryParams.applyTitle = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认取消申请记录编号为"' + ids + '"的数据项？').then(function() {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.getList1();
        this.$modal.msgSuccess("取消成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('work/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.table-container {
  height: 390px;
  overflow-y: auto;
  position: relative;
  margin-bottom: 10px;
}
</style>

